package cn.com.cetccst.uicomponents.binding.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.com.cetccst.uicomponents.R;

public class AlignItemView extends LinearLayout {

    private static final int GRAVITY_LR = 0, GRAVITY_LL = 1;
    private int valueHintColor;
    private String valueHint;
    private int leftIconId;
    private int valueSize;
    private int rightIconId;
    private int valueColor;
    private int gravity;
    private int keySize;
    private int leftPadding;
    private String key;
    private String value;
    private int keyColor;

    private TextView valueText;
    private TextView keyText;
    private ImageView rightImage;
    private ImageView leftImage;

    private OnValueChangedListener onValueChangedListener;

    public interface OnValueChangedListener {
        void onValueChanged(String value);
    }

    public AlignItemView(Context context) {
        super(context);
    }

    public AlignItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.AlignItemView);
        key = arr.getString(R.styleable.AlignItemView_key);
        value = arr.getString(R.styleable.AlignItemView_value);
        valueHint = arr.getString(R.styleable.AlignItemView_value_hint);
        keySize = arr.getDimensionPixelSize(R.styleable.AlignItemView_key_size, getResources().getDimensionPixelSize(R.dimen.common_ui_align_item_font_size));
        valueSize = arr.getDimensionPixelSize(R.styleable.AlignItemView_value_size, getResources().getDimensionPixelSize(R.dimen.common_ui_align_item_sub_font_size));
        leftPadding = arr.getDimensionPixelSize(R.styleable.AlignItemView_left_padding, getResources().getDimensionPixelSize(R.dimen.common_ui_align_item_L_R_padding));
        keyColor = arr.getColor(R.styleable.AlignItemView_key_color, getResources().getColor(android.R.color.black));
        valueColor = arr.getColor(R.styleable.AlignItemView_value_color, getResources().getColor(R.color.common_ui_align_item_gray));
        valueHintColor = arr.getColor(R.styleable.AlignItemView_value_hit_color, getResources().getColor(R.color.common_ui_align_item_gray));
        gravity = arr.getInt(R.styleable.AlignItemView_contentGravity, GRAVITY_LR);
        rightIconId = arr.getResourceId(R.styleable.AlignItemView_right_icon, 0);
        leftIconId = arr.getResourceId(R.styleable.AlignItemView_left_icon, 0);
        if (isInEditMode()) {
            inflateLayout(context);
            return;
        }
        inflateLayout(context);
        arr.recycle();
    }

    private void inflateLayout(Context context) {
        switch (gravity) {
            case GRAVITY_LR:
                LayoutInflater.from(context).inflate(R.layout.align_lr_kv_item, this);
                break;
            case GRAVITY_LL:
                LayoutInflater.from(context).inflate(R.layout.align_ll_kv_item, this);
                break;
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        keyText = findViewById(R.id.key);
        keyText.setText(key);
        keyText.setTextColor(keyColor);
        keyText.setTextSize(TypedValue.COMPLEX_UNIT_PX, keySize);

        rightImage = findViewById(R.id.right_icon);
        leftImage = findViewById(R.id.left_icon);
        valueText = findViewById(R.id.value);
        valueText.setTextColor(valueColor);
        valueText.setHintTextColor(valueHintColor);
        if (!TextUtils.isEmpty(value)) {
            valueText.setText(value);
            valueText.setTextSize(TypedValue.COMPLEX_UNIT_PX, valueSize);
        }
        if (!TextUtils.isEmpty(valueHint)) {
            valueText.setHint(valueHint);
            valueText.setTextSize(TypedValue.COMPLEX_UNIT_PX, valueSize);
        }
        if (rightIconId > 0) {
            rightImage.setVisibility(VISIBLE);
            rightImage.setImageResource(rightIconId);
        }
        if (leftIconId > 0) {
            leftImage.setVisibility(VISIBLE);
            leftImage.setImageResource(leftIconId);
            leftImage.setPadding(0, 0, leftPadding, 0);
        }
    }

    public void setOnValueChangedListener(OnValueChangedListener listener) {
        onValueChangedListener = listener;
    }

    public void setValue(String value) {
        valueText.setText(value);
        if (onValueChangedListener != null) {
            onValueChangedListener.onValueChanged(value);
        }
    }
    public void setValueMultiLine() {
        valueText.setGravity(Gravity.END);
        valueText.setSingleLine(false);

    }
    public String getValue() {
        return valueText.getText().toString();
    }

    public void setKey(String value) {
        keyText.setText(value);
    }

    public String getKey() {
        return keyText.getText().toString();
    }

    public void showRightIcon(boolean show) {
        if (show) {
            rightImage.setVisibility(VISIBLE);
        } else {
            rightImage.setVisibility(INVISIBLE);
        }
    }

    public void setKeyColor(int colorId) {
        keyText.setTextColor(getResources().getColor(colorId));
    }

    public void setValueColor(int colorId) {
        valueText.setTextColor(getResources().getColor(colorId));
    }

    public void setTextIsSelected(boolean bool) {
        keyText.setTextIsSelectable(bool);
        valueText.setTextIsSelectable(bool);
    }

    public void setKeyTextIsSelected(boolean bool) {
        keyText.setTextIsSelectable(bool);
    }

    public void setValueTextIsSelected(boolean bool) {
        valueText.setTextIsSelectable(bool);
    }

    public void showRight(boolean bool) {
        if (bool) {
            valueText.setVisibility(VISIBLE);
        } else {
            valueText.setVisibility(GONE);
        }
        showRightIcon(bool);
    }

    public void setValueBackgrouond(int drawableId) {
        valueText.setBackgroundResource(drawableId);
    }
}
